package com.engine.modules.speapi.mapper;


import com.engine.common.util.ListMapAndJson;
import com.engine.modules.general.mapper.GeneralDataCommonMapper;
import com.engine.modules.raster.mapper.RasterCommonMapper;
import com.engine.modules.vector.mapper.VectorSourceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
@Slf4j
public class SpecialGisMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private VectorSourceMapper vectorSourceMapper;

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    private RasterCommonMapper rasterCommonMapper;

    @Autowired
    private GeneralDataCommonMapper generalDataCommonMapper;

    public List<Map<String, Object>> getLocation(String pac) {
        String sql="SELECT id,dqbm,dqmc,parentbm,levels FROM b_gis_point_xzqh WHERE parentbm='"+pac+"'";
        return jdbcTemplate.queryForList(sql);
    }

    public List<Map<String, Object>> getLocationPolygon(String pac) {
        String sql = "SELECT id,name,pac,location_lvl,ST_AsGeoJson(geom) as geojson FROM b_gis_polygon_location_all WHERE pac=?";
        return jdbcTemplate.queryForList(sql, pac);
    }

    public List<Map<String, Object>> getLocationPolygonByYCPac(String ycpac) {
        String sql = "SELECT id,name,pac,ST_AsGeoJson(geom) as geojson FROM b_gis_polygon_location_cun WHERE ycpac=?";
        return jdbcTemplate.queryForList(sql, ycpac);
    }

    public List<Map<String, Object>> getLocationPacByLngLat(float lng, float lat) {
        String sql="SELECT id,name,pac,location_lvl FROM b_gis_polygon_location_all " +
                "WHERE st_intersects(geom, st_setsrid(st_point(?, ?),4326))";
        return jdbcTemplate.queryForList(sql,lng,lat);
    }

    public Integer updateQqcgPropertiesByGeojson(String pac, String geojson) {
        String tableName="b_gis_polygon_qqcg_x"+pac;
        Map<String, Object> map= ListMapAndJson.Geojson2ListMap(geojson).get(0);
        Set<String> set = map.keySet();            //得到由键组成的Set集合
        Iterator<String> it = set.iterator();
        List<Object> lars=new ArrayList<>();
        String optSeq="";
        while (it.hasNext()) {
            String key = it.next();
            if(key=="id"||key=="gmt_update"||key=="gmt_create"||key=="del_flag"||key=="geom")
                continue;
            if(map.get(key)==null)
                continue;
            if(key.contains("time"))
            {
                try{
                    lars.add(new Date((Long) map.get(key)));
                }
                catch (Exception e) {
                    log.error("出错了，存的空:"+e.getMessage(),e);
                    lars.add(null);
                }
                optSeq+=key+"=?,";
                continue;
            }
            optSeq+=key+"=?,";
            lars.add(map.get(key));
        }
        lars.add(map.get("geom"));
        optSeq=optSeq.substring(0, optSeq.length()-1);
        Object[] args=new Object[lars.size()];
        for (int k=0;k<lars.size();k++) {
            args[k]=lars.get(k);
        }
        String sql = "update "+tableName+
                " set gmt_update=NOW()," + optSeq+
                " where ST_Intersects(geom, st_geomfromgeojson(?)) AND del_flag='0'";
        int count = jdbcTemplate.update(sql,args);
        return count;
    }

    public String selectMultiTypeGeojsonByGeometry(String tableNames, String geometry,String access) {
        String[] tableNamesArry=tableNames.split(",");
        String geojsonAll="";
        for (String tablename:tableNamesArry) {
            List<Map<String, Object>> mapList = vectorSourceMapper.selectGeojsonByGeometry(geometry, tablename, access);
            for (Map<String, Object> map:mapList) {
                map.put("tableName",tablename);
            }
            String geojson = ListMapAndJson.ListMap2Geojson(mapList);
            geojsonAll=ListMapAndJson.GeojsonMergeFeatures(geojsonAll,geojson,tablename);
        }
        return geojsonAll;
    }

    public List<Map<String, Object>> selectStatisticalByGeometry(String statisticalNames, String rasterNames,String geometry, String access) throws Exception {
        List<Map<String, Object>> statisticalMaps = generalDataCommonMapper.selectStatisticalDataByGeometry(statisticalNames, geometry, access);
        if(statisticalMaps==null)
            statisticalMaps=new ArrayList<>();
        List<Map<String, Object>> rasterMaps = rasterCommonMapper.selectRasterAvgValueByGeometrySmart(rasterNames,geometry,1,2000000.0);
        for (Map<String, Object> map:rasterMaps) {
            statisticalMaps.add(map);
        }
        return statisticalMaps;
    }


}
